package com.example.administrator.liveapp.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.administrator.liveapp.R;
import com.example.administrator.liveapp.bean.MyArticleBean;
import com.example.administrator.liveapp.bean.MyCommentBean;
import com.example.administrator.liveapp.utils.TimeChangeUtil;

import java.util.List;

/**
 * Created by Administrator on 2017/6/21 0021.
 */

public class MyArticleRecyclerViewAdapter extends RecyclerView.Adapter<MyArticleRecyclerViewAdapter.ViewHolder> implements View.OnClickListener{

    public Context context;
    public List<MyArticleBean.ArticleBean> myArticle;
    public MyArticleRecyclerViewAdapter(List<MyArticleBean.ArticleBean> myArticle) {
        this.myArticle = myArticle;
    }

    @Override
    public MyArticleRecyclerViewAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.my_article_recy_item,null);
        ViewHolder viewHolder = new ViewHolder(view);
        context = parent.getContext();
        //将创建的View注册点击事件
        view.setOnClickListener(this);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(MyArticleRecyclerViewAdapter.ViewHolder holder, int position) {
        holder.articleTitle.setText(myArticle.get(position).getArticletitle());
        holder.article_content.setText(myArticle.get(position).getArticledescribe());
        String time = TimeChangeUtil.getStrTime2(myArticle.get(position).getArticletime());
        holder.article_time.setText(time);
        holder.article_comments.setText(myArticle.get(position).getArticlecommcount());
        holder.article_viewcount.setText(myArticle.get(position).getArticleviewcount());
        holder.article_best_number.setText(myArticle.get(position).getArticlebestcount());
        //将position保存在itemView的Tag中，以便点击时进行获取
        holder.itemView.setTag(position);
    }

    @Override
    public int getItemCount() {
        if(myArticle==null){
            return 0;
        }else{
            return myArticle.size();
        }
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        public TextView article_content;
        public TextView article_time;
        public TextView articleTitle;
        public TextView article_comments;
        public TextView article_viewcount;
        public TextView article_best_number;
        public ViewHolder(View itemView) {
            super(itemView);
            article_content = (TextView) itemView.findViewById(R.id.article_content);
            article_time = (TextView) itemView.findViewById(R.id.article_time);
            articleTitle = (TextView) itemView.findViewById(R.id.articleTitle);
            article_comments = (TextView) itemView.findViewById(R.id.article_comments);
            article_viewcount = (TextView) itemView.findViewById(R.id.article_viewcount);
            article_best_number = (TextView) itemView.findViewById(R.id.article_best_number);
        }
    }

    @Override
    public void onClick(View v) {
        if (mOnItemClickListener != null) {
            //注意这里使用getTag方法获取position
            mOnItemClickListener.onItemClick(v,(int)v.getTag());
        }
    }
    private MyArticleRecyclerViewAdapter.OnItemClickListener mOnItemClickListener = null;

    //define interface
    public static interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

    public void setOnItemClickListener(MyArticleRecyclerViewAdapter.OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }
}
